/******************************************************************************
Author: Akshay Dixit
Date Created: October 22, 2018
Data last modified: June 17, 2020
File Name: 5_tz.do
Project: T4D Tanzania 
Purpose: Analysis of Facility-level intermediate outcomes (Facility data)
******************************************************************************/

/* LIST OF INTERMEDIATE OUTCOMES & THE ASSOCIATED VARIABLES
		
	Note:	The sample has 153 facilities. But there are 152 observations in the facility dataset. 
			This is because 1 treatment facility could not be surveyed. This facility had been closed.
			In addition, only partial data is available from one other dispensary. 

Category 2. Improved facility access (transportation, new facility, longer facility hours, outreach services)
	
	Outcome 1:	Request ambulance - Whether the facility has a functional ambulance.
				amkp24 amkp25 amkp26 amkp27
	
Category 3. Improved information transparency (cost, opening hours, etc.) or complaint mechanisms

	Outcome 1:	Cost transparency - If cost information for delivery and other services is displayed publically.
				pi06, pi06a
	Outcome 2:	Hours transparency - If information on facility operating hours is displayed publically.
				kf11
	Outcome 3:	Complaint mechanism - 
				i. If the facility has a complaint management system
				pi09 pi10
				ii. If the facility conducts routine meetings with community members in the service area to identify how to improve quality of services
				pi11 pi12

Category 4. Bylaws, partnerships, or other interventions aimed at health system uptake

	Outcome 1:	If the dispensary or health center referred to by the dispensary has a by-law or other local regulation, requiring/encouraging the uptake of MNH services.
				pi18
	Outcome 2:	Midwife-TBA partnerships - If there are midwife partnerships with TBAs
				pi08a
	Outcome 3:	If there were any campaigns/programs to educate TBAs, or encourage them to not deliver babies at home. 
				pi08b			

Category 6. Improved attitude, effort, trust of the provider

	Outcome 1:	Provider perception of community problems
				pp14* pp15* pp16* pp17*
	Outcome 2:	Provider perception of community feedback and information levels
				pp22 pp23 pp24 pp25 pp26 pp27
				
Category 7. Increased or improved facility staffing

	Outcome 1:	Number of staff at the facility
				sd01 sd02
	Outcome 2:	Number of volunteers at the facility
				sd03
	Outcome 2:	Vacancies for staff
				sd04 sd05

Category 8. Improved facility infrastructure

	Outcome 1:	Electricity
				amkp01 amkp02 amkp03 amkp04 amkp05 amkp06 amkp06a
	Outcome 2:	Telecommunications and computer/internet
				amkp11 amkp12 amkp13 amkp14 amkp15 amkp16 amkp17 amkp18 amkp19 amkp20 amkp21
	Outcome 3:	Water
				amkp08 amkp09 amkp10 amkp10_x
	Outcome 4:	Delivery room
				amkp26 amkp27 amkp27_1 amkp27_2 amkp28 amkp28_ot amkp29 amkp30
	Outcome 5:	Toilet
				amkp33 amkp33_ot amkp34_a amkp34_b amkp34_c amkp34_d amkp34_e amkp35
	Outcome 6:	Communication			
				amkp36 amkp37 amkp37_ot

Category 9. Improved facility cleanliness
	
	Outcome 1:	Observed level of cleanliness of the facility delivery room and toilet 
				amkp31* amkp34*
				
Category 10. Increased availability of drugs, supplies and other inputs
	
	Outcome 1:	Number of essential medicines/equipment for mothers that the facility has in stock.
				ks03A-ks03P
	Outcome 2:	Number of essential medicines/equipment for children that the facility has in stock.
				ks04A-ks04N
	Outcome 3:	Number of essential vaccines that the facility has in stock.
				ks05A-ks05F
	Outcome 4:	Number of essential supplies that the facility has in stock.
				ks06A-ks06H
	Outcome 5:	Number of essential equipment that the facility has in stock.
				ks07A-ks07O				
				
Category 11. Improved provider knowledge

	Outcome 1:	Number of questions the midwife can correctly answer questions on healthcare practices.
				pp01 pp02 pp03
				
*/

clear all
set more off
	
u "$data/T4D_end_fc_clean.dta", clear

cd "$analysis"

gl strata = "strata1 strata2 strata3 strata4 strata5 strata6 strata7"
	
*******************************************************************************

* CATEGORY 2. Improved facility access

g functional_ambulance = (amkp24 == 1)
g access_to_vehicle = (amkp27 == 1)

	tab treatment amkp25
	tab treatment amkp26
	//Just 8 facilities reported having their own ambulance
	//As such, running regressions on fuel availability adds no value.

***Results***

*Label variables
lab var functional_ambulance "Facility has functional ambulance"
lab var access_to_vehicle "Facility has access to vehicle for emergencies"
	
*Produce Excel output

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 2 - Facility") modify

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A8=("Number of facilities") ///
A9=("Number of villages") B8=(76) C8=(77) B9=(100) C9=(100) ///
A11=("Treatment means are regression adjusted") A13=("*** p<0.01, ** p<0.05, * p<0.1") ///
A12=("Note: Outcomes starting with 'Facility' were analyzed for 153 sample facilities. Outcomes starting with 'Village' were analyzed for 200 sample villages") 

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
local outcomes functional_ambulance access_to_vehicle
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment $strata, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel F`row' = (_b[treatment]/r(sd))
	putexcel G`row' = (e(N))
	local ++row
}


*******************************************************************************

* CATEGORY 3. Improved information transparency (cost, opening hours, etc.) or complaint mechanisms

desc pi06* kf11* pi09* pi10* pi11* pi12*

***Preparing Outcome 1***
replace pi06 = . if pi06 == 6 		//Facilities with no delivery unit are excluded from this analysis
replace pi06 = 0 if pi06 == 2
tab treatment if pi06 == .			//A majority of these facilities are treatment facilities (8 of 11)
tab pi06

replace pi06b = 0 if pi06b == 2
tab pi06b

***Preparing Outcome 2***
tab kf11
ren kf11 hours_transparency
replace hours_transparency = 0 if hours_transparency == 2
tab hours_transparency

***Preparing Outcome 3***
tab pi09
tab pi10_1	
tab pi10_2	//Suggestion boxes, largely.
ren pi09 complaint_system
replace complaint_system = 0 if complaint_system == 2
tab complaint_system

tab pi11 
tab pi12	
g number_community_meetings = pi12
replace number_community_meetings = 0 if pi11 == 2	
list resp_pos* if number_community_meetings == .d
replace number_community_meetings = 0 if number_community_meetings == .d	
tab number_community_meetings
							
								//Don't know replaced as 0. 
								//The respondents were Clinical Officers and Nurses.
								//If they don't know that community meetings happened, the meetings may as well not happen.

***Results***

*Label variables
lab var pi06 "Information on cost of delivery services displayed"
lab var pi06b "Information on cost of other services displayed"
lab var hours_transparency "Information on facility hours displayed"
lab var complaint_system "Presence of complaint management system"
lab var number_community_meetings "Number of meetings with community members in the past year"


*Produce Excel output

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 3") modify

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A9=("Number of facilities") ///
A10=("Number of villages") B9=(76) C9=(77) B10=(100) C10=(100) ///
A12=("Treatment means are regression adjusted") A14=("*** p<0.01, ** p<0.05, * p<0.1") ///
A13=("Note: Outcomes starting with 'Facility' were analyzed for 153 sample facilities. Outcomes starting with 'Village' were analyzed for 200 sample villages") 

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
//Effect sizes are exported separately (see below)
local outcomes pi06 pi06b hours_transparency complaint_system number_community_meetings
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment $strata, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel F`row' = (_b[treatment]/r(sd))
	putexcel G`row' = (e(N))
	local ++row
}

*******************************************************************************

* CATEGORY 4. Bylaws, partnerships, or other interventions aimed at health system uptake

desc pi18* pi08a* pi08b*
foreach var in pi18 pi08a pi08b {
	replace `var' = 0 if `var' == 2
	tab `var'
}


***Regressions & Tabulations***

foreach var in pi18 pi08a pi08b {
	tab treatment, sum(`var')
	reg `var' treatment $strata, vce(robust)
}


***Output Tables***

*Label variables
lab var pi18 "Facility: Health center/dispensary has by-law on MNH services"
lab var pi08a "Facility: Dispensary has partnership with TBAs"
lab var pi08b "Facility: Presence of programs/campaigns to educate TBAs"

	
*Produce Excel output
cd "$analysis"

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 4") modify

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A7=("Number of facilities") ///
A8=("Number of villages") B7=(76) C7=(77) B8=(100) C8=(100) ///
A10=("Treatment means are regression adjusted") A12=("*** p<0.01, ** p<0.05, * p<0.1") ///
A11=("Note: Outcomes starting with 'Facility' were analyzed for 153 sample facilities. Outcomes starting with 'Village' were analyzed for 200 sample villages")

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
local outcomes pi18 pi08a pi08b
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment $strata, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel F`row' = (_b[treatment]/r(sd))
	putexcel G`row' = (e(N))
	local ++row
}


*******************************************************************************

* CATEGORY 6. Improved attitude, effort, trust of the provider

list health_facility_id lk* if pp14 == .	
//Data on this section is missing for one dispensary where only a partial survey was done

***Outcome 1***

desc pp14* pp15* pp16* pp17*

tab pp14, sort
tab pp14b

tab pp15, sort
tab pp15b

tab pp16, sort
tab pp16b

tab pp17_1, sort
tab pp17_2, sort
tab pp17_3, sort


***Preparing Outcome 2***
foreach var in pp22 pp23 pp24 pp25 pp26 pp27 {
	tab `var' 
}

		//Re-orienting variables so that higher values represent "better" outcomes
		local flip pp22 pp23 pp24 pp27
		foreach var of local flip {
			lab drop `var'
			tab `var'
			replace `var' = 5 - `var'
			tab `var'
		}


***Results***

*Label variables
lab var pp22 "Facility: Women are well aware of the MNH services offered at [dispensary name]"
lab var pp23 "Facility: Women seeking MNH understand the problems in running [dispensary name]"
lab var pp24 "Facility: Community members consulted when [dispensary] makes a decision on MNH"
lab var pp25 "Facility: In general, maternal health patients do not have meaningful feedback"
lab var pp26 "Facility: Most patients don’t appreciate the effort put in by the staff"
lab var pp27 "Facility: Patients communicate about their feedback on MNH services"


*Produce Excel output

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 6_facility") modify

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A12=("Number of facilities") ///
A13=("Number of villages") B12=(76) C12=(77) B13=(100) C13=(100) ///
A15=("Treatment means are regression adjusted") A18=("*** p<0.01, ** p<0.05, * p<0.1") ///
A16=("Outcomes starting with 'Facility' were analyzed for 153 sample facilities. Outcomes starting with 'Village' were analyzed for 200 sample villages") ///
A17=("All outcomes oriented such that higher values represent 'better' outcomes")

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
local outcomes pp22 pp23 pp24 pp25 pp26 pp27
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment $strata, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel F`row' = (_b[treatment]/r(sd))
	putexcel G`row' = (e(N))
	local ++row
}

*******************************************************************************

* CATEGORY 7. Increased or improved facility staffing

desc sd01 sd02 sd03 sd04 sd05
foreach var in sd01 sd02 sd03 sd04 sd05 {
	tab `var'
}


***Preparing Outcome 1***
g number_staff_supposed = sd01
g number_staff_actual = sd02

***Preparing Outcome 2***
g number_volunteers = sd03


***Preparing Outcome 3***
g any_staff_vacancies = (sd04 == 1)
g number_staff_vacancies = sd05
replace number_staff_vacancies = 0 if sd04 == 2


***Communication***
desc amkp38* amkp39*
foreach var in amkp38 amkp39_1 amkp39_2 amkp39_3 amkp39b {
	tab `var'
}

g mnh_staff_always_present = (amkp38 == 1)
tab mnh_staff_always_present
g mnh_contact_mobilephone = (amkp39_1 == 1)
tab mnh_contact_mobilephone

foreach var in mnh_staff_always_present mnh_contact_mobilephone {
	tab treatment, sum(`var')
	reg `var' treatment $strata, vce(robust)
}

***Results***

*Label variables
lab var number_staff_supposed "Number of paid medical staff supposed to work at facility"
lab var number_staff_actual "Number of paid medical staff who actually work at facility"
lab var number_volunteers "Number of volunteers who work at facility"
lab var any_staff_vacancies "Whether there are any staff vacancies"
lab var number_staff_vacancies "Number of staff vacancies"
lab var mnh_staff_always_present "Skilled delivery staff present/on call 24*7"
lab var mnh_contact_mobilephone "Facility staff can be contacted by cellphone"


*Produce Excel output

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 7") modify

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A11=("Number of facilities") ///
A12=("Number of villages") B11=(76) C11=(77) B12=(100) C12=(100) ///
A14=("Treatment means are regression adjusted") A16=("*** p<0.01, ** p<0.05, * p<0.1") ///
A15=("All outcomes analyzed for 153 sample facilities")

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
local outcomes number_staff_supposed number_staff_actual number_volunteers any_staff_vacancies number_staff_vacancies mnh_staff_always_present mnh_contact_mobilephone
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment $strata, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel F`row' = (_b[treatment]/r(sd))
	putexcel G`row' = (e(N))
	local ++row
}

*******************************************************************************	

* CATEGORY 8. Improved facility infrastructure

***Electricity***
desc amkp01* amkp02* amkp03* amkp04* amkp05* amkp06*
foreach var in amkp01 amkp02 amkp03 amkp04_1 amkp04_2 amkp04b amkp05 amkp06 amkp07 {
	tab `var'
}

g connected_main_grid = (amkp01 == 1)
g uninterrupted_electricity = (amkp02 == 1)	//Facilities not connected to the grid at all are marked as 0
g other_sources_electricity = (amkp03 == 1)
g functional_solar_panel = (amkp07 == 1)


***Telecommunications and computer/internet***
desc amkp13 amkp14 amkp15 amkp16 amkp17 amkp18 amkp19 amkp20 amkp21 amkp22 amkp23
foreach var in amkp13 amkp14 amkp15 amkp16 amkp17 amkp18 amkp19 amkp20 amkp21 amkp22 amkp23 {
	tab `var'
}

g functional_telephone = (amkp13 == 1 & amkp15 == 1)
g functional_radio = (amkp16 == 1 & amkp18 == 1)
g functional_computer = (amkp19 == 1 & amkp21 == 1)
g functional_email = (amkp22 == 1 & amkp23 == 1)
	
	//The number of observed/unobserved cases does not differ across treatment & control
	foreach var in amkp14 amkp17 amkp20 {
		tab treatment, sum(`var')
		reg `var' treatment, vce(robust)
	}

	
***Water***
desc amkp09 amkp09b amkp10 amkp11 amkp12 amkp12b
foreach var in amkp09 amkp09b amkp10 amkp11 amkp12 amkp12b {
	tab `var'
}

g water_onsite = (amkp10 == 1)
g water_shortage = (amkp11 == 1)
g water_shortage_period = .
	replace water_shortage_period = amkp12b if amkp12 == 1
	replace water_shortage_period = amkp12b*30 if amkp12 == 2
	
	
***Delivery room***
desc amkp28 amkp29 amkp30 amkp30b amkp31 amkp32_1 amkp32_2 amkp32b 
foreach var in amkp28 amkp29 amkp30 amkp30b amkp31 amkp32_1 amkp32_2 amkp32b {
	tab `var'
}

g specific_delivery_room = (amkp28 == 1)

replace amkp30 = . if amkp30 == 95
ren amkp30 privacy_delivery_room

ren amkp31 number_delivery_bed
replace number_delivery_bed = 0 if amkp28 == 2


***Maternity home***
ren amkp32a maternity_home
tab treatment, sum(maternity_home)


***Construction of placenta pit***
desc amkp34 amkp34b
tab amkp34
tab amkp34b

g placenta_pit = (amkp34 == 4)
tab placenta_pit


***Toilet***
desc amkp35 amkp35b amkp37
foreach var in amkp35 amkp35b amkp37 {
	tab `var'
}

g good_toilet_strict = (amkp35 == 1 | amkp35 == 2)
tab good_toilet_strict
g good_toilet = (amkp35 == 1 | amkp35 == 2 | amkp35 == 3 | amkp35 == 4 | amkp35 == 5 | amkp35 == 6 | amkp35 == 8)
tab good_toilet

g toilet_accessible = (amkp37 == 1)
tab toilet_accessible

lab drop amkp36e
g toilet_privacy = 4 - amkp36e
	
	
***Results***

*Label variables
lab var connected_main_grid "Facility is connected to the central electricity grid"
lab var uninterrupted_electricity "Facility had uninterrupted electricity during opening hours in last 7 days"
lab var other_sources_electricity "Facility has other sources of electricity besides central grid"
lab var functional_solar_panel "Facility solar power system is functional"
lab var functional_telephone "Facility has functional telephone" 
lab var functional_radio "Facility has functional radio"
lab var functional_computer "Facility has functional computer"
lab var functional_email "Facility has functional email"
lab var water_onsite "Water outlet available onsite"
lab var water_shortage "Routine water shortage"
lab var water_shortage_period "Duration of water shortage (in days)"
lab var specific_delivery_room "Facility has specific room for deliveries" 
lab var privacy_delivery_room "Privacy level of delivery room (1-4 scale)"
lab var number_delivery_bed "Number of designated delivery beds"
lab var maternity_home "Facility has maternity home or resting place for women"
lab var placenta_pit "Facility has placenta pit"
lab var good_toilet_strict "Delivery patient toilet: Flushed to septic tank/through piped system"
lab var toilet_accessible "Toilet inside/next to delivery room"
lab var toilet_privacy "Delivery patient toilet - Privacy (1-3 scale)"
	
	
*Produce Excel output

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 8") modify

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A22=("Number of facilities") ///
A23=("Number of villages") B22=(76) C22=(77) B23=(100) C23=(100) ///
A25=("Treatment means are regression adjusted") A27=("*** p<0.01, ** p<0.05, * p<0.1") ///
A26=("All outcomes analyzed for 153 sample facilities")

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
local outcomes connected_main_grid uninterrupted_electricity other_sources_electricity functional_solar_panel /// 
functional_telephone functional_radio functional_computer functional_email ///
water_onsite water_shortage water_shortage_period specific_delivery_room privacy_delivery_room number_delivery_bed ///
maternity_home placenta_pit good_toilet_strict toilet_accessible toilet_privacy
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment $strata, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel F`row' = (_b[treatment]/r(sd))
	putexcel G`row' = (e(N))
	local ++row
}

	
*******************************************************************************	

* CATEGORY 9. Improved facility cleanliness

desc amkp33* amkp36*

foreach var in amkp33a amkp33b amkp33c amkp33d amkp33e amkp36a amkp36b amkp36c amkp36d {
	tab `var'
}

ren amkp33a delivery_room_bed
ren amkp33b delivery_room_floor
ren amkp33c delivery_room_ventilation
ren amkp33d delivery_room_waste_disposal
ren amkp33e delivery_room_dust

ren amkp36a toilet_water_flush
ren amkp36b toilet_water_handwash
ren amkp36c toilet_soap_handwash
ren amkp36d toilet_cleanliness

***Preparing delivery room outcome***
replace delivery_room_bed = 0 if (delivery_room_bed == 2 | delivery_room_bed == 3)
replace delivery_room_floor = 0 if (delivery_room_floor == 2 | delivery_room_floor == 3)
	//Creating a dirty/clean binary variable for delivery_room_floor

replace delivery_room_ventilation = 0 if delivery_room_ventilation == 2
replace delivery_room_waste_disposal = 0 if delivery_room_waste_disposal == 2

replace delivery_room_dust = 0 if delivery_room_dust == 1
replace delivery_room_dust = 1 if delivery_room_dust == 2
	//Orienting the variable so that "1" represents the positive outcome

***Preparing toilet outcomes***
replace toilet_water_flush = 0 if toilet_water_flush == 2
replace toilet_water_handwash = 0 if toilet_water_handwash == 2
replace toilet_soap_handwash = 0 if toilet_soap_handwash == 2

lab drop amkp36d
tab toilet_cleanliness 
replace toilet_cleanliness  = 4 - toilet_cleanliness 
tab toilet_cleanliness 		


***Output Tables***

*Label variables
lab var delivery_room_bed "Delivery bed is clean"
lab var delivery_room_floor "Delivery room floor is clean"
lab var delivery_room_ventilation "Delivery room is well ventilated"
lab var delivery_room_waste_disposal "Biological/medical waste disposal available"
lab var delivery_room_dust "No dust or mold observed in the room"
lab var toilet_water_flush "Delivery patient toilet - Water is available to flush"
lab var toilet_water_handwash "Delivery patient toilet - Water to wash hands"
lab var toilet_soap_handwash "Delivery patient toilet - Soap to wash hands"
lab var toilet_cleanliness "Delivery patient toilet - Cleanliness (1-3 scale)"

	
*Produce Excel output
	
//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 9_facility") modify

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A13=("Number of facilities") ///
A14=("Number of villages") B13=(76) C13=(77) B14=(100) C14=(100) ///
A16=("Treatment means are regression adjusted") A18=("*** p<0.01, ** p<0.05, * p<0.1") ///
A17=("All outcomes analyzed for 153 sample facilities")

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
local outcomes delivery_room_bed delivery_room_floor delivery_room_ventilation delivery_room_waste_disposal ///
delivery_room_dust toilet_water_flush toilet_water_handwash toilet_soap_handwash toilet_cleanliness
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment $strata, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel F`row' = (_b[treatment]/r(sd))
	putexcel G`row' = (e(N))
	local ++row
}

*******************************************************************************	

* CATEGORY 10. Increased availability of drugs, supplies and other inputs

desc ks03* ks04* ks05* ks06* ks07*

***Preparing Outcome 1***
local ks03 ks03a ks03b ks03c ks03d ks03e ks03f ks03g ks03h ks03i ks03j ks03k ks03l ks03m ks03n ks03o ks03p
foreach var of local ks03 {
	tab `var'
	g `var'_dummy = (`var' == 1)
	replace `var'_dummy = . if `var' == .
}

egen number_medicines_mother = rowtotal(ks03a_dummy-ks03p_dummy)
tab number_medicines_mother
drop *_dummy


***Preparing Outcome 2***
local ks04 ks04a ks04b ks04c ks04d ks04e ks04f ks04g ks04h ks04i ks04j ks04k ks04l ks04m ks04n ks04o
foreach var of local ks04 {
	tab `var'
	g `var'_dummy = (`var' == 1)
	replace `var'_dummy = . if `var' == .
}

egen number_medicines_child = rowtotal(ks04a_dummy-ks04o_dummy)
tab number_medicines_child
drop *_dummy


***Preparing Outcome 3***
local ks05 ks05a ks05b ks05c ks05d ks05e ks05f ks05g ks05h
foreach var of local ks05 {
	tab `var'
	g `var'_dummy = (`var' == 1)
	replace `var'_dummy = . if `var' == .
}

egen number_vaccines = rowtotal(ks05a_dummy-ks05h_dummy)
tab number_vaccines
drop *_dummy


***Preparing Outcome 4***
local ks06 ks06a ks06b ks06c ks06d ks06e ks06f ks06g ks06h ks06i
foreach var of local ks06 {
	tab `var'
	g `var'_dummy = (`var' == 1)
	replace `var'_dummy = . if `var' == .
}

egen number_equipment = rowtotal(ks06a_dummy-ks06i_dummy)
tab number_equipment
drop *_dummy


***Preparing Outcome 5***
local ks07 ks07a ks07b ks07c ks07d ks07e ks07f ks07g ks07h ks07i ks07j ks07k ks07l ks07m ks07n ks07o
foreach var of local ks07 {
	tab `var'
	g `var'_dummy = (`var' == 1)
	replace `var'_dummy = . if `var' == .
}

egen number_supplies = rowtotal(ks07a_dummy-ks07o_dummy)
tab number_supplies
drop *_dummy


***Output Tables***

*Label variables
lab var number_medicines_mother "Number of essential medicines for mothers in stock"
lab var number_medicines_child "Number of essential medicines for children in stock"
lab var number_vaccines "Number of essential vaccines in stock"
lab var number_equipment "Number of essential equipment available & functional"
lab var number_supplies "Number of essential supplies in stock"


*Produce Excel output

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 10") modify

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A8=("Number of facilities") ///
A9=("Number of villages") B8=(76) C8=(77) B9=(100) C9=(100) ///
A11=("Treatment means are regression adjusted") A13=("*** p<0.01, ** p<0.05, * p<0.1") ///
A12=("All outcomes analyzed for 153 sample facilities")

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
local outcomes number_medicines_mother number_medicines_child number_vaccines number_equipment number_supplies
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment $strata, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel F`row' = (_b[treatment]/r(sd))
	putexcel G`row' = (e(N))
	local ++row
}

*******************************************************************************	

* CATEGORY 11. Improved provider knowledge

desc pp09* pp10* pp11* pp12* pp13*

foreach var in pp09 pp10 pp11_1 pp11_2 pp11_3 pp11_4 pp11_5 pp11_6 pp11b pp12 pp12b ///
pp13_1 pp13_2 pp13_3 pp13_4 pp13_5 pp13_6 pp13_7 pp13b {
	tab `var'
}

//ANC
g anc_right_answer = (pp09 == 4)
replace anc_right_answer = . if pp09 == .
tab anc_right_answer

g anc_timing = (pp12 == 4 | pp12 == 1)
replace anc_timing = . if pp12 == . 


//Facility birth
g facility_birth_right_answer = (pp10 == 4)
replace facility_birth_right_answer = . if pp10 == .
tab facility_birth_right_answer

foreach var in pp11_1 pp11_2 pp11_3 pp11_4 pp11_5 pp11_6 {
	tab `var'
	g `var'_binary = (`var' != .)
	replace `var'_binary = . if pp11_1 == .
	tab `var'_binary
}

egen reason_refer_hospital = rowtotal(pp11_1_binary pp11_2_binary pp11_3_binary pp11_4_binary pp11_5_binary pp11_6_binary), missing
tab reason_refer_hospital
drop *_binary


//Vaccinations
foreach var in pp13_1 pp13_2 pp13_3 pp13_4 pp13_5 pp13_6 pp13_7 {
	tab `var'
	g `var'_binary = (`var' != .)
	replace `var'_binary = . if pp13_1 == .
	tab `var'_binary
} 

egen vaccine = rowtotal(pp13_1_binary pp13_2_binary pp13_3_binary pp13_4_binary pp13_5_binary pp13_6_binary pp13_7_binary), missing
tab vaccine
drop *_binary


***Output Tables***

*Label variables
lab var anc_right_answer "Provider responded correctly to question on whether to seek ANC"
lab var anc_timing "Provider responded correctly to question on ANC timing" 
lab var facility_birth_right_answer "Provider responded correctly to delivery question"
lab var reason_refer_hospital "Number of reasons given to refer woman to hospital for birth"
lab var vaccine "Number of vaccines named for an infant should be given"


*Produce Excel output

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 11") modify

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A8=("Number of facilities") ///
A9=("Number of villages") B8=(76) C8=(77) B9=(100) C9=(100) ///
A11=("Treatment means are regression adjusted") A13=("*** p<0.01, ** p<0.05, * p<0.1") ///
A12=("All outcomes analyzed for 153 sample facilities")

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
local outcomes anc_right_answer anc_timing facility_birth_right_answer reason_refer_hospital vaccine
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment $strata, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel F`row' = (_b[treatment]/r(sd))
	putexcel G`row' = (e(N))
	local ++row
}

*******************************************************************************

clear
